import {
  configure,
  defineRule,
  ErrorMessage,
  Field as VeeField,
  Form as VeeForm,
} from 'vee-validate';

import {
  alpha_spaces as alphaSpaces,
  confirmed,
  email,
  max,
  max_value as maxVal,
  min,
  min_value as minVal,
  not_one_of as excluded,
  required,
} from '@vee-validate/rules';

export default {
  install(app) {
    app.component('VeeForm', VeeForm);
    app.component('VeeField', VeeField);
    app.component('ErrorMessage', ErrorMessage);
    defineRule('required', required);
    defineRule('tos', required);
    defineRule('min', min);
    defineRule('max', max);
    defineRule('alpha_spaces', alphaSpaces);
    defineRule('email', email);
    defineRule('min_value', minVal);
    defineRule('max_value', maxVal);
    defineRule('passwords_mismatch', confirmed);
    defineRule('excluded', excluded);
    defineRule('country_excluded', excluded);

    configure({
      generateMessage: (context) => {
        const messages = {
          // required: `The field ${context.field} is required.`,
          required: '必填项,请填写',
          // min: `The field ${context.field} is too short.`,
          min: '输入内容太短了,请重写填写',
          // max: `The field ${context.field} is too long.`,
          max: '输入内容太长了,超过最大值,请重写填写',
          // alpha_spaces: `The field ${context.field} may only contain alphabetical characters`,
          alpha_spaces: '此项只能为字母和空格,请重新填写',
          // email: `The field ${context.field} must be a valid email.`,
          email: '邮箱格式不对,请重写填写',
          // min_value: `The field ${context.field} is too low.`,
          min_value: '太低了,不合法,请重写填写',
          // max_value: `The field ${context.field} is too high.`,
          max_value: '太高了,不合法,请重写填写',
          // excluded: `You are not allowed to use this value for the field ${context.field}.`,
          excluded: '你不允许用这个值',
          // country_excluded: 'Due to restrictions, we do not accept users from this location',
          country_excluded: '根据国际关系法,我们暂时对此国家不开放注册',
          // passwords_mismatch: "The passwords don't match.",
          passwords_mismatch: '密码输入不匹配',
          // tos: 'You must accept the Terms of Services.',
          tos: '你必须同意我们的合同和条款才能注册',
        };
        return messages[context.rule.name]
          ? messages[context.rule.name] : `The field ${context.field} is invalid.`;
      },
      validateOnBlur: true,
      validateOnChange: true,
      validateOnInput: false,
      validateOnModelUpdate: true,
    });
  },
};
